-- Advert 首充，特权，基金等活动的主界面Tips

local ClassRef = app.mvc.createRenderClass(...)

function ClassRef.create(node)
    local sprite = ClassRef.extend(cc.CSLoader:createNode(Res.AdvertRender))
    if node == nil then
		ccui.Helper:doLayout(sprite)
	end
    sprite:init()
    return sprite
end

function ClassRef:init(data)
    self.super.init(self, data)

    local function onClick()
        if self._subType then
            if tonumber(self._subType) == 1001 then
                UIHandler:handleMessage({id=Constant.FUNC_TYPE_LOTTERY, mode="open"})
                self:removeNode()
            elseif tonumber(self._subType) == 1002 then
                -- 指定为奇迹卡片
                UIHandler:handleMessage( { id = Constant.FUNC_TYPE_SHOPDRAW, mode = "open"})
                self:removeNode()
            else
                local RewardModel = app:getInst("RewardModel")
                local info = RewardModel:getWelfareActivityBySubType(tonumber(self._subType))
                if info then
                    app:sendMsg("RewardController", "openView", info)
                    self:removeNode()
                end
            end
        end
    end
    display.uiAddClick(self._ui.Panel, onClick)
    
    self:initAction()
end

function ClassRef:refresh()
    if self._data then
        self._subType = self._data.subType -- 当前活动的subType
        if self._data.open_ui then
            self._ui.Image:loadTexture(string.format( "%s%s.png", Res.newerBattleGuideClassRoleBg, self._data.open_ui))
        end
        if self._data.open_string then
            self._ui.Text:setString(L(self._data.open_string))
            self._ui.Text:getVirtualRenderer():setLineSpacing(-5)
        end
        self:playInAction()
        if self._subType then
            local dailyTimes = UD:getValueFromGenericByKey("payNoticeDailyTimes"..self._subType) or 0
            UD:saveGeneric("payNoticeDailyTimes"..self._subType, tonumber(dailyTimes)+1)
            UD:saveGeneric("payNoticeLastTime"..self._subType, app.session:getServerTime())
        end
    end
end

function ClassRef:initAction()
    if not self.mAction then
        self.mAction = cc.CSLoader:createTimeline(Res.AdvertRender)
        if self.mAction then
            self:runAction(self.mAction)
            self.mAction:gotoFrameAndPause(0)
        end
    end
end

function ClassRef:playInAction()
    if self.mAction then
        self.mAction:clearLastFrameCallFunc()
        self.mAction:play("in", false)
        self.mAction:setLastFrameCallFunc(function()
            self:playBreathAction()
        end)
    end
end

function ClassRef:playBreathAction()
    if self.mAction then
        self.mAction:play("breath", true)
    end
end

function ClassRef:removeNode()
    local DesktopController = UIHandler:getDesktopController()
    if DesktopController then
        local desktop = DesktopController:getDesktop()
        if desktop then
            desktop:removeAdvertNode()
        end
    end
end

return ClassRef